package com.buyouchina.service.product.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.buyouchina.bean.area.Province;
import com.buyouchina.bean.product.WeekAccount;
import com.buyouchina.bean.product.Account.AccountAreaType;
import com.buyouchina.bean.product.Account.AccountType;
import com.buyouchina.service.base.DaoSupport;
import com.buyouchina.service.product.WeekAccountService;
import com.buyouchina.utils.ProvinceUtil;

@Service
public class WeekAccountServiceBean extends DaoSupport<WeekAccount> implements
		WeekAccountService {

	String hql;

	public WeekAccount checkRecord(Integer productid, String provinceid, Date date,Integer weekOfYear) {
		List<WeekAccount> accounts=new ArrayList<WeekAccount>();
		if(provinceid!=null){
			hql = "select s from Account s "
				+ "where s.date=?1 and s.product.id=?2 and s.province.id=?3 and s.weekOfYear=?4 ";
			accounts=list(hql,date, productid,Integer.parseInt(provinceid),weekOfYear);
		}else{
			hql = "from Account s "
				+ "where s.product.id=?1 and s.date=?2  and s.weekOfYear=?3 and s.province.id is null";
			accounts=this.list(hql,productid,date,weekOfYear);
		}
		if (accounts.size() > 0) {
			return accounts.get(0);
		}
		return null;
	}
	
	public List<WeekAccount> list(Date date,AccountAreaType type,Integer provinceid){
		if(provinceid!=null){
			hql="from Account a where a.date=?1 and a.areaType=?2 and a.province.id=?3";
			return list(hql,date, type,provinceid);
			
		}else{
			hql="from Account a where a.date=?1 and a.areaType=?2 ";
			return list(hql,date, type);
		}
	}
	/*
	public List<WeekAccount> listByProduct(Integer productid,AccountAreaType areaType,AccountType type,Date date,Province p){
		
		if(p!=null){
			hql="from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4 and s.province.id between ?5 and ?6";
			return list(hql, productid,areaType,type,date,p.getId(),ProvinceUtil.getLastIndexOfProvince(p.getId()));
		}
		hql="from Account s where s.product.id=?1 and s.areaType=?2 and s.type=?3 and s.date=?4";
		return list(hql, productid,areaType,type,date);
		
	}
	*/
	public List<WeekAccount> list(Date startTime,Date endTime,Integer provinceid){
		if(provinceid!=null){
			hql="from Account a where a.date=?1 and a.areaType=?2 and a.province.id=?3";
			return list(hql,startTime, endTime,provinceid);
			
		}else{
			hql="from Account a where a.date=?1 and a.areaType=?2 ";
			return list(hql,startTime, endTime);
		}
	}

	public List<WeekAccount> listByProduct(Integer productid,
			AccountAreaType areaType, AccountType type, Date date, Province p) {
		// TODO Auto-generated method stub
		return null;
	}
}
